package top.fangwenmagician.calculationstring.operator;

import top.fangwenmagician.calculationstring.ProcessControl;
import top.fangwenmagician.calculationstring.exception.CalculationException;

import java.math.BigDecimal;

/**
 * 加法运算符
 */
public class Add extends A_NormalRegularOperator {
    @Override
    public String regularText() {
        return "([\\d.]+)\\+([\\d.]+)";
    }

    @Override
    public int priority() {
        return 50;
    }

    @Override
    public String calculation(String[] captureStrings, ProcessControl processControl) throws Exception {
        if (captureStrings.length >= 3) {
            String s1 = captureStrings[1];
            String s2 = captureStrings[2];
            try {
                Double.parseDouble(s1);
            } catch (Exception e) {
                throw new CalculationException("\"" + s1 + "\"无法转换为double", e);
            }
            try {
                Double.parseDouble(s2);
            } catch (Exception e) {
                throw new CalculationException("\"" + s2 + "\"无法转换为double", e);
            }
            return new BigDecimal(s1).add(new BigDecimal(s2)).toString();
        } else {
            throw new CalculationException("未捕获到足够的数据，请检查表达式是否错误");
        }
    }
}
